1 00:00:00,390 --> 00:00:04,170 I want to implement a daily STREAKS system into my game. 2 00:00:04,170 --> 00:00:08,370 So if a player plays five days in a row, my game will know it. 3 00:00:08,370 --> 00:00:09,510 Something will pop up. 4 00:00:09,510 --> 00:00:14,100 Say congrats, maybe give them a prize, you know, per day or something. 5 00:00:14,100 --> 00:00:18,330 But with that in place, we can also figure out daily rewards. 6 00:00:18,330 --> 00:00:20,880 So we're going to know when there was a day rollover. 7 00:00:20,880 --> 00:00:22,590 You can give him a reward for that. 8 00:00:22,590 --> 00:00:24,330 That's very popular in games. 9 00:00:24,450 --> 00:00:30,420 And finally, with this system in place, we're going to know if it's the first time somebody played. 10 00:00:30,420 --> 00:00:35,430 So then we could use that information to pop up instructions for first time players. 11 00:00:35,430 --> 00:00:37,530 So let's go ahead and get started with that. 12 00:00:38,070 --> 00:00:43,740 Go to server script service and then open up the game manager in server script service. 13 00:00:43,740 --> 00:00:49,140 And we have our function ad board, which is slightly misleading because we do add the board, but we 14 00:00:49,140 --> 00:00:52,650 have other player info that does not go on the board. 15 00:00:52,740 --> 00:00:56,130 We're going to add two more values to that player info. 16 00:00:56,130 --> 00:01:03,240 We're going to say local saved type time equals instance new. 17 00:01:03,270 --> 00:01:12,390 It's going to be a number value and that information is going to go on player info and let's give saved 18 00:01:12,390 --> 00:01:18,120 time a name I'm going to call it saved time. 19 00:01:18,120 --> 00:01:19,230 All one word. 20 00:01:19,590 --> 00:01:23,340 I'm not going to do this space because then I can't use the dot notation. 21 00:01:23,790 --> 00:01:24,020 All right. 22 00:01:24,050 --> 00:01:28,680 I need another variable to I'm going to do one for streak, right? 23 00:01:28,710 --> 00:01:32,070 That's my daily streak instance new. 24 00:01:32,070 --> 00:01:35,460 That's an int value because it's going to be like one, two, three, four. 25 00:01:35,490 --> 00:01:38,160 That's also going to go on the player info. 26 00:01:39,090 --> 00:01:49,170 We will give that name streak, dot name, capital streak. 27 00:01:49,840 --> 00:01:54,050 Let's scroll down to initialize player data. 28 00:01:54,070 --> 00:01:55,430 There it is. 29 00:01:55,450 --> 00:01:58,030 Now we have two situations. 30 00:01:58,030 --> 00:02:01,330 The player might have data and this is an update, right? 31 00:02:01,330 --> 00:02:03,220 So we may have people playing already. 32 00:02:03,220 --> 00:02:04,930 We don't want to reset their data. 33 00:02:04,930 --> 00:02:08,590 We have to take into account for their old data and then we have new people. 34 00:02:08,590 --> 00:02:10,300 That's not going to be a problem. 35 00:02:10,300 --> 00:02:11,740 As soon as they come in. 36 00:02:11,740 --> 00:02:13,720 We can give them two new values. 37 00:02:13,720 --> 00:02:22,210 We can give them the streak which will set the one, and then we have our saved time and then that will 38 00:02:22,210 --> 00:02:28,540 set to OSS time, which is the number of seconds after midnight of January 1st, 1970. 39 00:02:28,540 --> 00:02:29,860 Epoch All right. 40 00:02:29,860 --> 00:02:32,320 But this is going to be a problem. 41 00:02:33,430 --> 00:02:35,200 Let's go ahead and check. 42 00:02:35,500 --> 00:02:38,830 Do they have streak data or don't they have streak data? 43 00:02:38,830 --> 00:02:43,000 Because we're only going to have to initialize that one time if they don't have it. 44 00:02:43,000 --> 00:02:50,300 So I'm going to say, if not data dot streak that's been saved off. 45 00:02:50,300 --> 00:02:57,160 Right, then what I'm going to do is I want to get my player, not the leader stats. 46 00:02:57,160 --> 00:03:05,290 I'm going to do the player info and I'm going to get my street value street value set that equal to 47 00:03:05,290 --> 00:03:06,210 one, right? 48 00:03:06,220 --> 00:03:08,080 Because this is brand new for these people. 49 00:03:08,080 --> 00:03:10,660 They just came in since our patch. 50 00:03:11,080 --> 00:03:17,980 Do a control C do a control V, this is going to be saved time saved time. 51 00:03:17,980 --> 00:03:23,530 That value is also going to be O's dot time. 52 00:03:24,700 --> 00:03:24,970 All right? 53 00:03:24,970 --> 00:03:28,090 And then the next time they come in, they're already going to have that data. 54 00:03:28,090 --> 00:03:29,620 So we don't want to do that. 55 00:03:29,620 --> 00:03:31,360 We're going to do an else. 56 00:03:32,260 --> 00:03:33,820 Let's copy this. 57 00:03:34,210 --> 00:03:36,460 I will put real values there now. 58 00:03:36,460 --> 00:03:36,880 Right. 59 00:03:36,880 --> 00:03:38,500 So now we don't need that one. 60 00:03:38,500 --> 00:03:41,320 We could do a data dot streak. 61 00:03:41,320 --> 00:03:48,310 It's going to be there and we'll do a data dot saved time. 62 00:03:49,540 --> 00:03:50,020 All right. 63 00:03:50,020 --> 00:03:51,100 That's looking good. 64 00:03:52,450 --> 00:03:54,870 We want to check here for our daily streak. 65 00:03:54,880 --> 00:04:01,380 Let's do a check, daily streak and we'll pass in the player. 66 00:04:01,390 --> 00:04:02,860 We don't have that function yet. 67 00:04:02,860 --> 00:04:03,960 I think I'll copy it. 68 00:04:03,970 --> 00:04:07,390 Control C so I don't have to worry about spelling it wrong. 69 00:04:07,390 --> 00:04:11,170 As long as you're above initialized player data, you'll be all right. 70 00:04:11,170 --> 00:04:16,470 I'm going to go below the ad board function, right. 71 00:04:16,540 --> 00:04:18,310 I just want it closer to the top. 72 00:04:18,550 --> 00:04:23,170 Let me do a local function check daily streak. 73 00:04:23,170 --> 00:04:24,700 I just did a paste there. 74 00:04:25,270 --> 00:04:26,860 Let's get a couple variables. 75 00:04:26,860 --> 00:04:31,390 I'll do one for saved time. 76 00:04:31,480 --> 00:04:37,540 We'll go get our player, get the player info from the player, and then we're going to get the saved 77 00:04:37,570 --> 00:04:39,850 time dot value. 78 00:04:40,960 --> 00:04:49,870 I'm going to need the seconds per day and that's 86400 seconds in a day. 79 00:04:51,640 --> 00:04:52,480 What else do I need? 80 00:04:52,480 --> 00:04:56,110 I need a variable for now. 81 00:04:56,110 --> 00:05:00,400 The time now I'm going to use O's time. 82 00:05:00,400 --> 00:05:08,200 That's going to give me the number of seconds passed January 1st, 1970, which is Unix epoch. 83 00:05:08,230 --> 00:05:15,310 It's down to the millisecond, but Lua uses the uses seconds for the unit. 84 00:05:15,760 --> 00:05:24,850 All right, I'm going to do another variable called I'll call this now minus one day, and that's going 85 00:05:24,850 --> 00:05:28,360 to be now minus seconds per day. 86 00:05:28,360 --> 00:05:34,060 So since now as in some sort of second units since 1970. 87 00:05:34,270 --> 00:05:38,770 Subtracting off 86,400 puts us back a day. 88 00:05:39,670 --> 00:05:40,990 I need two more variables. 89 00:05:40,990 --> 00:05:45,310 I need one for the formatted saved time. 90 00:05:45,310 --> 00:05:49,900 Those seconds are great, but it's kind of tough for days and stuff like that. 91 00:05:49,900 --> 00:05:53,200 So let's use OS day to format our time. 92 00:05:53,200 --> 00:05:57,700 We have that little single quote star DT. 93 00:05:57,730 --> 00:06:02,410 That means we're going to make a table with saved data. 94 00:06:02,410 --> 00:06:04,150 So we're passing the second send. 95 00:06:04,150 --> 00:06:11,620 We're going to get back right here a table with things like the year, the day, the month, the hour 96 00:06:11,620 --> 00:06:13,270 is going to be very convenient. 97 00:06:13,480 --> 00:06:15,880 We're going to need another one for R. 98 00:06:15,910 --> 00:06:28,900 We'll call this formatted now minus one day O's date star T for our table. 99 00:06:29,950 --> 00:06:33,040 And we're going to put now minus one day in there. 100 00:06:33,970 --> 00:06:37,360 Now we can make comparisons on days really easy. 101 00:06:37,720 --> 00:06:38,860 Let's do that. 102 00:06:40,000 --> 00:06:41,800 I'm going to do an if statement right here. 103 00:06:41,800 --> 00:06:48,520 I'm going to say if formatted save time dot why day? 104 00:06:48,520 --> 00:06:58,480 So why day is the day of the year between one and 365 on a normal year one and 366 on a leap year. 105 00:06:58,480 --> 00:07:01,270 But that way I don't have to worry about months and stuff like that. 106 00:07:01,270 --> 00:07:04,300 I have a nice consecutive ordering of the days. 107 00:07:04,570 --> 00:07:11,950 I'll say equals equals formatted now minus one day. 108 00:07:12,400 --> 00:07:15,820 Why day then? 109 00:07:16,390 --> 00:07:24,460 Well, if I subtract a day and the last time I logged in is that same day, I'm going to say that's 110 00:07:24,460 --> 00:07:27,490 a good candidate to increase my daily streak. 111 00:07:27,490 --> 00:07:41,830 So if I have my player dot player info dot streak, dot value plus equals one, that should be pretty 112 00:07:41,830 --> 00:07:42,550 good. 113 00:07:42,550 --> 00:07:47,140 We have some more stuff that to check though, but we don't want that to happen. 114 00:07:47,230 --> 00:07:53,070 We're going to call this periodically, so we don't want this to happen more than once during gameplay 115 00:07:53,320 --> 00:07:54,730 for the same day. 116 00:07:54,730 --> 00:08:03,940 So what I'm going to do is I can get my player player info saved time, dot value equals. 117 00:08:03,940 --> 00:08:12,730 Now then when I save that off, this is going to fail because there's no way that now is the same as 118 00:08:12,730 --> 00:08:14,470 now minus one day. 119 00:08:14,470 --> 00:08:17,620 But we have a few more things to check for. 120 00:08:17,620 --> 00:08:26,380 We have our days good, but we also have to make sure that it's not like a year and a day later. 121 00:08:26,380 --> 00:08:27,430 Let's go ahead. 122 00:08:27,430 --> 00:08:29,890 And why did it do random? 123 00:08:29,890 --> 00:08:33,220 Let's do an and get rid of that. 124 00:08:33,220 --> 00:08:34,750 Go to the next line. 125 00:08:35,380 --> 00:08:40,240 I'm going to do another check to make sure that the years are equal. 126 00:08:43,160 --> 00:08:46,250 So we'll do a year here. 127 00:08:47,950 --> 00:08:49,570 And a year here. 128 00:08:49,570 --> 00:08:52,370 What happens on year rollover? 129 00:08:52,390 --> 00:08:55,420 Well, we went back a day. 130 00:08:55,420 --> 00:09:01,600 So it should be like if if it's December 31st, 2021. 131 00:09:01,600 --> 00:09:02,020 Right. 132 00:09:02,020 --> 00:09:06,100 And we're playing January 1st, we're going to subtract a day. 133 00:09:06,100 --> 00:09:12,610 So these should both be 2021, these should both be 365. 134 00:09:12,610 --> 00:09:16,120 I don't think it was a leap year, but that part will be. 135 00:09:16,120 --> 00:09:20,800 OC Let's go ahead and put this then on the same line. 136 00:09:22,600 --> 00:09:25,330 Let's add two more conditions to our F statement. 137 00:09:25,330 --> 00:09:34,780 So the first condition we are one day apart from our save time and our log in time are now time we award 138 00:09:34,780 --> 00:09:39,190 another value to the streak, but we'll do an LCF. 139 00:09:39,190 --> 00:09:47,680 What happens if the now time minus the save time is actually less than seconds per day? 140 00:09:47,710 --> 00:09:51,970 Let's do less than or equal to just to include that last second. 141 00:09:51,970 --> 00:09:53,830 Well, nothing happens, right? 142 00:09:53,890 --> 00:09:56,530 Because what's going to happen we're going to get down here. 143 00:09:56,620 --> 00:09:59,470 The save time is going to be updated to now time. 144 00:09:59,470 --> 00:10:00,490 We're going to be playing. 145 00:10:00,490 --> 00:10:02,410 We're going to call this many times. 146 00:10:02,410 --> 00:10:04,120 Let's just write. 147 00:10:04,930 --> 00:10:06,550 Playing the same day. 148 00:10:06,550 --> 00:10:09,430 Playing the same day. 149 00:10:10,780 --> 00:10:11,580 All right. 150 00:10:11,590 --> 00:10:14,800 If this is if this fails. 151 00:10:14,800 --> 00:10:15,080 Right. 152 00:10:15,100 --> 00:10:16,770 So we're not a day apart. 153 00:10:16,780 --> 00:10:21,400 If this fails, we are not within 24 hours now. 154 00:10:21,400 --> 00:10:22,680 We'll break the streak. 155 00:10:22,690 --> 00:10:32,290 So I'll say else, just grab this instead of adding the one where you're going to set it equal to one. 156 00:10:32,290 --> 00:10:34,080 And that's going to break our streak. 157 00:10:34,090 --> 00:10:36,370 Let's get a print statement in here. 158 00:10:38,260 --> 00:10:49,240 We'll say added to the streak and then I will put the value of the streak, the new value. 159 00:10:50,500 --> 00:10:51,280 Cool. 160 00:10:51,280 --> 00:10:53,740 And then here we're going to break the streak. 161 00:10:53,770 --> 00:10:54,580 Where should we do it? 162 00:10:54,580 --> 00:10:55,120 Underneath. 163 00:10:55,120 --> 00:10:57,970 Yeah, we'll just say broke the streak. 164 00:10:57,970 --> 00:10:59,530 You broke your streak 165 00:11:04,240 --> 00:11:05,620 with an exclamation. 166 00:11:05,620 --> 00:11:06,700 We don't want to miss it. 167 00:11:06,700 --> 00:11:08,230 Lots of exclamations. 168 00:11:08,530 --> 00:11:10,090 All right, so this is the boring one. 169 00:11:10,090 --> 00:11:11,950 This is the one that's going to happen the most. 170 00:11:12,040 --> 00:11:14,320 Let's scroll down a little bit. 171 00:11:15,480 --> 00:11:21,030 And in our save player data. 172 00:11:21,660 --> 00:11:21,890 Right. 173 00:11:22,080 --> 00:11:23,250 We have to update. 174 00:11:23,490 --> 00:11:28,820 We're going to do an update every time we call save player data, which is periodic. 175 00:11:28,830 --> 00:11:31,560 It's in the save loop and when we leave. 176 00:11:31,950 --> 00:11:36,420 Another thing I'm going to do on that save loop, let's just increase this to 10 seconds because I'm 177 00:11:36,420 --> 00:11:41,460 getting a lot of warnings about the buffer getting filled with our save requests. 178 00:11:41,460 --> 00:11:45,330 So every 10 seconds, save player data is going to happen. 179 00:11:45,330 --> 00:11:49,590 We'll do our check daily streak, pass in the player. 180 00:11:50,100 --> 00:11:52,470 Then we'll get our data. 181 00:11:52,470 --> 00:11:57,690 And data has saved time, right? 182 00:11:58,410 --> 00:12:03,600 Player dot player info save time. 183 00:12:03,600 --> 00:12:13,500 And then we have the streak right streak player dot player info streak. 184 00:12:14,130 --> 00:12:18,810 Oh and these are values dot value dot value. 185 00:12:19,770 --> 00:12:21,740 So we're going to put that in our data. 186 00:12:21,780 --> 00:12:23,220 We're going to save it off. 187 00:12:23,430 --> 00:12:24,330 It'll be great. 188 00:12:24,330 --> 00:12:27,180 And then save loop is now 10 seconds. 189 00:12:27,210 --> 00:12:29,790 That's going to call our save player data. 190 00:12:29,820 --> 00:12:31,710 Let's try this for errors. 191 00:12:31,710 --> 00:12:37,110 You're not going to get exactly the same thing because I've tried this a few times off camera, but 192 00:12:37,110 --> 00:12:38,490 check for errors. 193 00:12:38,490 --> 00:12:42,420 I'll go to view output. 194 00:12:43,250 --> 00:12:45,140 And we can go ahead and clear that. 195 00:12:46,230 --> 00:12:47,220 Hit the play. 196 00:12:51,920 --> 00:12:53,070 And we're not getting expand. 197 00:12:53,220 --> 00:12:56,600 Our air's waves starting. 198 00:12:59,130 --> 00:13:01,040 Playing the same day. 199 00:13:01,050 --> 00:13:01,940 Very good. 200 00:13:01,950 --> 00:13:03,330 So we're playing the same day. 201 00:13:03,330 --> 00:13:04,740 We're not getting any errors. 202 00:13:04,740 --> 00:13:08,220 Let's go ahead and do a little dummy test. 203 00:13:08,220 --> 00:13:10,820 This is in no way comprehensive. 204 00:13:10,830 --> 00:13:13,200 You'll have to test this over and over. 205 00:13:13,200 --> 00:13:15,990 And then you're still going to have to really be careful about the year. 206 00:13:15,990 --> 00:13:16,970 Roll over. 207 00:13:16,980 --> 00:13:24,150 Let me go up to let's look at initialized player data and right here. 208 00:13:24,150 --> 00:13:27,180 So we're going to get data because we already saved it off. 209 00:13:27,180 --> 00:13:34,290 Let's subtract our 86400 seconds. 210 00:13:35,070 --> 00:13:35,370 All right? 211 00:13:35,370 --> 00:13:39,390 And that way we should increase by one day. 212 00:13:39,420 --> 00:13:40,680 Let's hit our play. 213 00:13:45,200 --> 00:13:46,370 Added two straight. 214 00:13:46,370 --> 00:13:47,810 So we have two. 215 00:13:48,230 --> 00:13:48,920 Good. 216 00:13:49,810 --> 00:13:52,450 What happens if we. 217 00:13:54,750 --> 00:13:56,320 Do this. 218 00:13:56,340 --> 00:14:00,780 Let's do three times. 219 00:14:02,430 --> 00:14:04,220 Don't forget, we saved off data. 220 00:14:04,230 --> 00:14:04,560 That was. 221 00:14:04,860 --> 00:14:06,810 That was a little bit older. 222 00:14:07,170 --> 00:14:09,420 We should blow our streak here. 223 00:14:12,270 --> 00:14:14,000 You broke your streak. 224 00:14:14,010 --> 00:14:15,000 All right. 225 00:14:15,390 --> 00:14:16,860 That's looking pretty good. 226 00:14:17,100 --> 00:14:19,380 I'm going to try and do it tomorrow. 227 00:14:19,410 --> 00:14:21,480 Log in before I post this video. 228 00:14:21,480 --> 00:14:26,940 I'll do a couple of days just to make sure there's no quirks in it, but I think we're good to go.